package wc.fileUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import wc.fileUtil.exception.RootDirectoryIsNotFolderException;

public class FileCollector {
	
	private List<File> _fileList = new ArrayList<File>();
	
	public FileCollector(String rootPath) throws RootDirectoryIsNotFolderException {
		File rootDir = new File(rootPath);
		if(!rootDir.isDirectory()) throw new RootDirectoryIsNotFolderException();
		readParent(rootDir);
	}
	
	public List<File> getFiles() {
		return _fileList;
	}
	
	public int getFileCount() {
		return _fileList.size();
	}
	
	private void addAll(List<File> files) {
		_fileList.addAll(files);
	}
	
	private boolean isParentContainDirectory(File parent) {
		for(File children : parent.listFiles()) {
			if(children.isDirectory()) {
				return true;
			}
		}
		return false;
	}
	
	private void readParent(File parent) {
		if(!isParentContainDirectory(parent)) {
			addAll(Arrays.asList(parent.listFiles()));
		} else {
			for(File child : parent.listFiles()) {
				readParent(child);
			}
		}
	}
}
